package com.broada.apm.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.broada.apm.ajax.PerformanceResult;
import com.broada.apm.base.BaseService;
import com.broada.apm.entity.AreaPerformance;
import com.broada.apm.entity.PerformanceDetail;
import com.broada.apm.entity.PerformanceGroup;
import com.broada.apm.model.PagingList;
import com.broada.apm.model.PerformanceView;
import com.broada.apm.service.PerformanceService;
import com.broada.apm.util.Constants;

public class PerformanceServiceImpl extends BaseService implements PerformanceService {


	/**
	 * appIds不为null 则查询app应用系统性能 appIds为null appId/nodeIds不为null 则查询节点性能
	 */
	@Override
	public PerformanceResult queryPerformanceByDate(String appIds, String appId, String appName, String agentIds, long startDate, long endDate, String size) {
		PerformanceResult result = new PerformanceResult();
		if ("NaN".equals(endDate) || "NaN".equals(startDate)) {
			result.setFailMessage("查询时间错误");
			return result;
		}
		String[] appIdArry = null;
		String[] agentIdArry = null;
		
		// 应用系统
		if (StringUtils.isNotBlank(appIds)) {
			appIdArry = decoderArry(appIds);
		} else if (StringUtils.isNotBlank(appId)) {
			appIdArry = decoderArry(appId);
		} else if (StringUtils.isNotBlank(appName) && StringUtils.isBlank(appId) && StringUtils.isBlank(appIds)) {
			com.broada.apm.model.ApplicationSystem applicaiton = appLogic.getApplicationByName(appName);
			if (applicaiton == null) {
				result.setFailMessage("应用系统不存在");
				return result;
			}
			appIdArry = decoderArry(applicaiton.getId());
		}
		if (appIdArry == null) {
			result.setFailMessage("参数错误");
			return result;
		}
		// 应用节点
	   if (StringUtils.isNotBlank(agentIds)) {
			agentIdArry = decoderArry(agentIds);
		}
		List<PerformanceGroup> groups = perfLogic.queryPerformance(appIdArry, agentIdArry, startDate, 
				endDate, size);
		result.setPerformanceGroup(groups);
		return result;
	}

	@Override
	public PerformanceResult queryPerformanceByDimensionType(String applicationId, String dimensionType, int top, String orderBy,
			long startDate, long endDate, String panelId) {
		PerformanceResult result = new PerformanceResult();
		if ("NaN".equals(endDate) || "NaN".equals(startDate)) {
			result.setFailMessage("查询时间错误");
			return result;
		}
		List<PerformanceView> list = perfLogic.queryPerformanceByDimensionType(applicationId, dimensionType, top, orderBy,
				startDate, endDate, panelId);
		
		for (PerformanceView perfView : list) {
			if (perfView.get("business/user/unique") == null) {
				perfView.put("business/user/unique", 0.0);
			}
			if (perfView.get("business/score/apdex") == null) {
				perfView.put("business/score/apdex", 0.0);
			}
		}
		result.setPerformances(list);
		return result;
	}

	@Override
	public PerformanceResult queryPerformanceByArea(String appId, String sortField, long startDate, long endDate) {
		PerformanceResult result = new PerformanceResult();
		if ("NaN".equals(endDate) || "NaN".equals(startDate)) {
			result.setFailMessage("查询时间错误");
			return result;
		}
		AreaPerformance perf = perfLogic.queryPerformanceByArea(appId, sortField, startDate, endDate);
		result.setMapType(perf.getMapType());
		result.setPerformancesResult(perf.getPerformances());
		return result;
	}

	@Override
	public PerformanceResult queryDetailsPerformanceByKeyTrade(String appId, String resId, long startDate, long endDate) {
		PerformanceResult result = new PerformanceResult();
		if ("NaN".equals(endDate) || "NaN".equals(startDate)) {
			result.setFailMessage("查询时间错误");
			return result;
		}
		PerformanceDetail detail = perfLogic.queryDetailsPerformanceByKeyTrade(appId, resId, new Date(startDate), new Date(
				endDate));
		result.addData(Constants.PERFORMANCES, detail.getPerformances());
		result.addData("errorCode", detail.getErrorCode());
		return result;
	}

	@Override
	public PerformanceResult queryDetailsPerformanceByLastDate(String appId, String resId, long lastDate) {
		PerformanceResult result = new PerformanceResult();
		PerformanceDetail detail = perfLogic.queryDetailsPerformanceByLastDate(appId, resId, new Date(lastDate));
		result.setPerformances(detail.getPerformances());
		result.setErrorCode(detail.getErrorCode());
		return result;
	}

	@Override
	public PerformanceResult queryPerformanceByPage(String appId, int top, String sortField, long startDate, long endDate) {
		PerformanceResult result = new PerformanceResult();
		if ("NaN".equals(endDate) || "NaN".equals(startDate)) {
			result.setFailMessage("查询时间错误");
			return result;
		}
		List<PerformanceView> resPerfs = perfLogic.queryPerformanceByPage(appId, top, sortField, startDate, endDate);
		result.setPerformancesResult(resPerfs);
		return result;
	}


	@Override
	public PerformanceResult queryPerformanceByConnection(String appId, String nodeId, long startDate, long endDate, int pageIndex,
			int pageSize) {
		PerformanceResult result = new PerformanceResult();
		if ("NaN".equals(endDate) || "NaN".equals(startDate)) {
			result.setFailMessage("查询时间错误");
			return result;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(endDate));
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		Date _startDate = calendar.getTime();
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		Date _endDate = calendar.getTime();
		PagingList<PerformanceView> perfsList = perfLogic.queryPerformanceByConnection(appId, nodeId, _startDate,
				_endDate, pageIndex, pageSize);
		result.pagingHandle(pageSize, pageIndex, perfsList.getData());
		return result;
	}

	@Override
	public PerformanceResult queryPerformanceByPageId(String appId, String resourceId, String field, String size, long startDate, long endDate) {
		PerformanceResult result = new PerformanceResult();
		if ("NaN".equals(endDate) || "NaN".equals(startDate)) {
			result.setFailMessage("查询时间错误");
			return result;
		}
		List<PerformanceView> perfs = perfLogic.queryPerformanceByPageId(appId, resourceId, field, size, new Date(startDate),
				new Date(endDate));
		result.setPerformancesResult(perfs);
		return result;
	}

	@Override
	public PerformanceResult queryPerformanceSubTotal(String appId, String type, String targetIds, String orderBy, boolean desc,
			int top, int pageIndex, long startDate, long endDate, String panelId) {
		PerformanceResult result = new PerformanceResult();
		if ("NaN".equals(endDate) || "NaN".equals(startDate)) {
			result.setFailMessage("查询时间错误");
			return result;
		}
		if (endDate == 0)
			endDate = System.currentTimeMillis();
		if (startDate == 0)
			startDate = endDate - 60 * 60 * 1000;
		if (StringUtils.isBlank(type) || StringUtils.isBlank(targetIds)) {
			result.setFailMessage("参数错误");
			return result;
		}

		List<PerformanceView> perfs = perfLogic.queryPerformanceSubTotal(appId, type, targetIds, new Date(startDate),
				new Date(endDate), orderBy, desc, panelId);
		result.pagingHandle(10, pageIndex, perfs);
		return result;
	}

	@Override
	public PerformanceResult queryDetailsPerformanceByTransaction(String appId, String tradeTypeName, long startDate,
			long endDate, String agentId) {
		PerformanceResult result = new PerformanceResult();
		if ("NaN".equals(endDate) || "NaN".equals(startDate)) {
			result.setFailMessage("查询时间错误");
			return result;
		}
		PerformanceDetail detail = perfLogic.queryDetailsPerformanceByTransaction(appId, tradeTypeName, new Date(startDate), new Date(
				endDate), agentId);
		result.addData(Constants.PERFORMANCES, detail.getPerformances());
		result.addData("errorCode", detail.getErrorCode());
		return result;
	}


//	@Override
//	public Result queryRelationPerformanceByServiceId(String serviceId, long startDate, long endDate) {
//		Result result = new Result();
//		List<Service> services = perfLogic.queryRelationPerformanceByServiceId(serviceId, new Date(startDate), new Date(endDate));
//		result.addData(Constants.NODES, services);
//		return result;
//	}
	

//	@Override
//	public PerformanceResult queryAppsPerformanceSubTotal(String appIds, long startDate, long endDate) {
//		if (endDate == 0)
//			endDate = System.currentTimeMillis();
//		if (startDate == 0)
//			startDate = endDate - 60 * 60 * 1000;
//		PerformanceResult result = new PerformanceResult();
//		if (StringUtils.isBlank(appIds)) {
//			result.setFailMessage("参数错误");
//			return result;
//		}
//
////		List<PerformanceGroup> perfs = perfLogic.queryPerformanceSubTotal(appIds, new Date(startDate), new Date(endDate));
////		result.setPerformanceGroup(perfs);
//		return result;
//	}
	
}
